﻿using System.Collections.Generic;
using System.Linq;
using K9Nano.Share.Exceptions;

namespace K9Nano.Configuration
{
    internal class SettingDefinitionManager : ISettingDefinitionManager
    {
        private readonly IDictionary<string, SettingDefinition> _settings;

        public SettingDefinitionManager()
        {
            _settings = new Dictionary<string, SettingDefinition>();
        }

        public void Create(IEnumerable<SettingDefinition> settings)
        {
            foreach (var setting in settings)
            {
                if (_settings.ContainsKey(setting.Name))
                {
                    throw new K9KeyAlreadyExistException("There is already a setting defined with name: " + setting.Name);
                }
                _settings.Add(setting.Name, setting);
            }
        }

        public SettingDefinition GetSettingDefinition(string name)
        {
            if (!_settings.TryGetValue(name, out var settingDefinition))
            {
                throw new KeyNotFoundException("There is no setting defined with name: " + name);
            }

            return settingDefinition;
        }

        public IReadOnlyList<SettingDefinition> GetAllSettingDefinitions()
        {
            return _settings.Values.ToList();
        }
    }
}